package com.zc.webnews.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zc.webnews.R;
import com.zc.webnews.adapter.NewsItemAdapter;
import com.zc.webnews.bean.NewsDetailsBean;
import com.zc.webnews.ui.ShowNewsActivity;
import com.zc.webnews.utils.HttpUrlUtils;
import com.zc.webnews.utils.XmlParseUtils;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


/**
 * 新闻Fragment
 * Created by Administrator on 2016/11/02.
 */


@SuppressLint("ValidFragment")
public class BaseNews extends Fragment implements AdapterView.OnItemClickListener {

    private View news;
    private static int TAG = 0;//上拉下拉标志位
    private int channel;//频道ID 一共16个频道
    private int index;//标志第几个新闻类别
    private String title;//新闻类别的标题
    private String url;//新闻链接
    private PullToRefreshListView pfl;
    private ListView listView;
    public  SparseBooleanArray sba = new SparseBooleanArray();
    private HttpUrlUtils httpUrlUtils;
    private XmlParseUtils xmlParseUtils;
    private List<NewsDetailsBean> list = new ArrayList<>();
    private NewsItemAdapter adapter;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0){
                adapter.notifyDataSetChanged();
                pfl.onRefreshComplete();
            }
        }
    };

    @SuppressLint("ValidFragment")
    public BaseNews(String title, String url, int channel, int index) {
        this.title = title;
        this.url = url;
        this.channel=channel;
        this.index=index;
//        Log.e("BaseNews--->",this.url+"--->"+System.currentTimeMillis());
    }

    public void setUrl(String url){
        this.url = url;
    }

    public void setChannel(int channel){
        this.channel = channel;
    }

    public void setIndex(int index){
        this.index = index;
    }
    public int getIndex(int index){
        return index;
    }

    public String getTitle(){
        return title;
    }


    protected void initData() {

        pfl.setMode(PullToRefreshBase.Mode.BOTH);
        listView = pfl.getRefreshableView();
        listView.setOnItemClickListener(this);
        adapter = new NewsItemAdapter(getActivity(), list);
        listView.setAdapter(adapter);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        Log.e("onActivityCreated--->","onActivityCreated");
           initData();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        news = inflater.inflate(R.layout.fragment_news, null);
        findById();
        initListener();
        getNewsData();
        return news;
    }

    private void initListener() {

       pfl.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
           @Override
           public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                TAG = 0;//下拉刷新
                getNewsData();
           }

           @Override
           public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
               TAG = 1;//上拉加载
               getNewsData();
           }
       });
    }

    public void getNewsData(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                httpUrlUtils = new HttpUrlUtils();
                InputStream input = httpUrlUtils.getInputStream(url);
                xmlParseUtils.pullParseItemXml(input,title);
                if (TAG == 0){
                    list.clear();
                    list.addAll(XmlParseUtils.list_details);
                }else {
                    list.addAll(XmlParseUtils.list_details);
                }
                handler.sendEmptyMessage(0);

            }
        }).start();
    }
    private void findById() {
        pfl = (PullToRefreshListView) news.findViewById(R.id.pfl);
        xmlParseUtils = new XmlParseUtils();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(),ShowNewsActivity.class);
        intent.putExtra("url",list.get(position-1).getUrl());
        startActivity(intent);
    }
}
